package com.anddict.action;

import java.io.File;
import java.util.ArrayList;

import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.anddict.util.MiscUtil;
import com.anddict.R;
import com.anddict.sql.DbAdapter;
import com.anddict.util.Reference;
import com.anddict.AbsAction;
import com.anddict.AndDict;

public class TabDatabase extends AbsAction implements OnClickListener,TextWatcher{

	Spinner spinnerDatabase;
	Button btLoad;
	Button btRefresh;
	EditText entryLimitCompletion;
	TextView textviewCurrentDatabase;
	ArrayList<String> alDatabase = new ArrayList<String>();
	
	public TabDatabase(AndDict activity, DbAdapter dbAdapter){
		
		super(activity, dbAdapter);
		
		Log.d(Reference.TAG, "TabDatabase.Constructor");
		spinnerDatabase = (Spinner) activity.findViewById(R.id.spinnerDatabase);
		
		btLoad = (Button)activity.findViewById(R.id.btLoad);
		btRefresh = (Button)activity.findViewById(R.id.btRefresh);
		btLoad.setOnClickListener(this);
		btRefresh.setOnClickListener(this);
		
		entryLimitCompletion = (EditText)activity.findViewById(R.id.entryLimitCompletion);
		entryLimitCompletion.addTextChangedListener(this);
		
		textviewCurrentDatabase = (TextView)activity.findViewById(R.id.textviewCurrentDatabase);
		
		refreshComboDb();

	}
	
	public void onClick(View v) {
		
		Log.d(Reference.TAG, "TabDatabase.onClick - " + v.toString() );
		
        // Perform action on click
		if (v.equals(btLoad)){
			loadDatabase();
		}else if(v.equals(btRefresh)){
			refreshDatabase();
		}
	}
	
	public void loadDatabase(){
		
		if (this.getDbAdapter().openDatabase(Reference.WORK_DIRECTORY + "/" + this.spinnerDatabase.getItemAtPosition(spinnerDatabase.getSelectedItemPosition()))){
			this.getActivity().showDialog(Reference.ALERT_DATABASE_OPEN);
			this.textviewCurrentDatabase.setText("Database loaded: " + this.spinnerDatabase.getItemAtPosition(spinnerDatabase.getSelectedItemPosition()));
			return;
		}else{
			this.getActivity().showDialog(Reference.ALERT_NO_DATABASE_OPEN);
			return;
		}
	}
	
	public void refreshDatabase(){
		refreshComboDb();
	}
	
	public void refreshComboDb(){
	    File dir = new File(Reference.WORK_DIRECTORY);
	    
	    String[] children = dir.list();
	    if (children == null) {
	        // Either dir does not exist or is not a directory
			this.getActivity().showDialog(Reference.ALERT_NO_COMPLETION);
			return;
	    } else {
	    	alDatabase = new ArrayList<String>();
	        for (int i=0; i<children.length; i++){
	        	if (children[i].endsWith(Reference.SUFFIX_DB)){
		            // Get filename of file or directory
		            alDatabase.add(children[i]);
	        	}
	    	}
	        
	    	ArrayAdapter<String> arrayAdapterCategory = new ArrayAdapter<String> (this.getActivity(), android.R.layout.simple_spinner_item,alDatabase);
	    	arrayAdapterCategory.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    	spinnerDatabase.setAdapter(arrayAdapterCategory);
	    }
	}
	
	public void afterTextChanged(Editable s){
		// entry completion should be between 1 and 20
		if (entryLimitCompletion.getText() != null || entryLimitCompletion.getText().toString().equals("") == false){
			this.getActivity().showDialog(Reference.ALERT_NO_COMPLETION);
			return;
		}
		
		try{
			int val = Integer.parseInt(entryLimitCompletion.getText().toString());
			
			if (val < 1 || val > 20){
				this.getActivity().showDialog(Reference.ALERT_NO_VALID_COMPLETION);
				return;	
			}
		}catch(NumberFormatException ex){
			Log.e(Reference.TAG, MiscUtil.getStackTrace(ex));
		}
	}
	
	public void beforeTextChanged(CharSequence s, int start, int count, int after){}
	public void onTextChanged(CharSequence s, int start, int before, int count){}

}
